package com.infore.dataStatistics.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.StatTargetUnion;
import com.infore.common.model.entity.vo.StatTargetUnionVo;
import com.infore.dataStatistics.main.service.StatTargetUnionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 目标数据源联合主键 前端控制器
 * </p>
 *
 * @author shaolay
 * @since 2022-12-13
 */
@RestController
@Api(value = "目标数据源联合主键前端控制器", tags = "目标数据源联合主键前端控制器")
@RequestMapping("/statTargetUnion")
public class StatTargetUnionController extends BaseController {
	private String prefix = "/statTargetUnion";

	@Autowired
	private StatTargetUnionService StatTargetUnionService;

	@GetMapping()
	public String StatTargetUnionController(){
		return prefix;
	}

	/**
	 * 查询目标数据源联合主键列表
	 * @param stattargetunionVo 目标数据源联合主键的vo类
	 * @return Msg<Object> 结果集
	 * @author shaolay
	 * @since 2022-12-13
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "查询目标数据源联合主键列表", notes = "目标数据源联合主键列表查询 ")
	public Msg<Object> list(StatTargetUnionVo stattargetunionVo){
		//sql查询条件
		Wrapper<StatTargetUnion> wrapper = null ;
		try {
			wrapper = (Wrapper<StatTargetUnion>) WrapperHelp.addCondition(new EntityWrapper<StatTargetUnion>(), stattargetunionVo);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(500,e.getMessage());
		}
		//查询方法
		Page<StatTargetUnion> eppPage = new EppPage<StatTargetUnion>(StatTargetUnionService,stattargetunionVo,wrapper);
		return ResultUtil.successPage(StatTargetUnionService.selectPage(eppPage, wrapper));
	}

	/**
	 * 查询目标数据源联合主键列表
	 * @param stattargetunion 目标数据源联合主键
	 * @return Msg<Object> 结果集
	 * @author shaolay
	 * @since 2022-12-13
	 */
	@ResponseBody
	@ApiOperation(value = "添加目标数据源联合主键", notes = "目标数据源联合主键新增")
	@RequestMapping(value="/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public Msg<Object> add(@RequestBody StatTargetUnion stattargetunion){
		Wrapper<StatTargetUnion> wrapper = new  EntityWrapper<StatTargetUnion>();
		wrapper.eq("id", stattargetunion.getId());

	wrapper.eq("1", "1");
		List<StatTargetUnion> entityList = StatTargetUnionService.selectList(wrapper);
		if(entityList.size()>0) {
			return ResultUtil.error("重复添加");
		}
	//插入数据
		boolean flag = StatTargetUnionService.insert(stattargetunion);
		return ResultUtil.setResult(flag,stattargetunion.getId().toString());
	}


	/**
	  * 更新目标数据源联合主键方法
	  *
	  * @param stattargetunion 目标数据源联合主键
	  * @return Msg<Object>
	  * @author shaolay
	  * @since 2022-12-13
	  */
	@ResponseBody
	@ApiOperation(value = "更新目标数据源联合主键", notes = "目标数据源联合主键更新")
	@RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Msg<Object> edit(@RequestBody StatTargetUnion stattargetunion) {
		//插入数据
		boolean flag = StatTargetUnionService.updateById(stattargetunion);
		return ResultUtil.setResult(flag, stattargetunion.getId().toString());
	}

	/**
	 * 删除目标数据源联合主键
	 *
	 * @param  id
	 * @return Msg<Object>
	 * @author shaolay
	 * @since 2022-12-13
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "删除目标数据源联合主键", notes = "删除目标数据源联合主键")
	public Msg<Object> del(String id) {

		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("id", id);
		boolean flag = StatTargetUnionService.deleteByMap(columnMap);
		return ResultUtil.setResult(flag);
	}
}
